package org.geekbang.thinking.in.spring.bean.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.util.ObjectUtils;

/**
 * {@link DestructionAwareBeanPostProcessor} 的实现
 *
 * @author: wangsiqi
 * @date: 2021/11/10
 */
public class MyDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor {

    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        if (ObjectUtils.nullSafeEquals(beanName, "userHolder") && bean.getClass().equals(UserHolder.class)) {
            // SmartInitializingSingleton#afterSingletonsInstantiated: v8 -> DestructionAwareBeanPostProcessor#postProcessBeforeDestruction: v9
            UserHolder userHolder = (UserHolder) bean;
            userHolder.setDesc("the user holder version9");
            System.out.println("postProcessBeforeDestruction(): " + userHolder.getDesc());
        }
    }

}
